class ListNode1 {
    int val;
    ListNode1 next = null;
    public ListNode1(int val) {
        this.val = val;
    }
}

public class Main {
    public static void main(String[] args) {
        ListNode1 l1=new ListNode1(1);
        ListNode1 l2=new ListNode1(2);
        ListNode1 l3=new ListNode1(3);
        ListNode1 l4=new ListNode1(4);
        ListNode1 l5=new ListNode1(5);
        l1.next=l2;
        l2.next=l3;
        l3.next=l4;
        l4.next=l5;
        ListNode1 qw1=oddEvenList(l1);
        while(qw1!=null){
            System.out.println(qw1.val);
            qw1=qw1.next;
        }
    }

    public static ListNode1 oddEvenList(ListNode1 head) {
        if (head == null) {
            return head;
        }
        // write code here
        ListNode1 p1 = new ListNode1(-1);
        ListNode1 p2 = new ListNode1(-1);
        ListNode1 cur = head;
        ListNode1 q1 = p1;
        ListNode1 q2 = p2;
        while (cur != null) {
            if (cur != null) {
                q1.next = cur;
                cur = cur.next;
                q1 = q1.next;
            }
            if (cur != null) {
                q2.next = cur;
                cur = cur.next;
                q2 = q2.next;
            }
        }
        q2.next=null;
        q1.next = p2.next;
        return p1.next;
    }
}